#include "widget.h"
#include "ui_widget.h"

#include<QNetworkReply>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("http请求");
    manager=new QNetworkAccessManager(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //获取到输入框的url,构造QUrl对象
    QUrl url(ui->lineEdit->text());
    //构造http请求对象
    QNetworkRequest request(url);
    //发送GET请求
    QNetworkReply*response=manager->get(request);
    //通过信号槽来处理响应
    connect(response,&QNetworkReply::finished,this,[=](){
        //响应正确
        if(response->error()==QNetworkReply::NoError)
        {
            QString html=response->readAll();
            ui->plainTextEdit->setPlainText(html);
        }
        else
        {
            ui->plainTextEdit->setPlainText(response->errorString());
        }
        response->deleteLater();
    });
}
